package com.coffee.ws.command;

import com.coffee.core.CoffeeResponseEnum;
import com.coffee.core.ResponseCode;
import com.coffee.ws.metadata.BitMessage;
import com.coffee.ws.metadata.BasicBinaryMessage;
import com.coffee.ws.metadata.CoffeeBinaryProtocol;
import lombok.Getter;
import lombok.Setter;

import java.util.List;


/**
 * 前端传过来的 MochaMessage  转换成 MochaCommand 对象
 *
 * @author: songkui
 * @since: 2023/2/21 18:05
 */
@Setter
@Getter
public class CoffeeBinaryParam<T extends BasicBinaryMessage> extends BasicParam {
    private Long userId;

    private byte type;  //消息类型  1, chat  2, Binary
    private int cmd;
    private T request;

    private CoffeeBinaryProtocol protocol;


    public CoffeeBinaryParam(CoffeeBinaryProtocol<T> protocol) {
        this.type = protocol.getType();
        this.cmd = protocol.getCmd();
        this.protocol = protocol;

    }


    public void sendMessage(ResponseCode code, BasicBinaryMessage message) {
        protocol.setState(code.code());
        protocol.setData(message);
        sendMessage(protocol);
    }
    public void sendMessage(Integer code, BasicBinaryMessage<T> message) {
        protocol.setState(code);
        protocol.setData(message);
        sendMessage(protocol);
    }

    public void sendMessage(BasicBinaryMessage message) {
        sendMessage(CoffeeResponseEnum.Success, message);
    }

    public void sendSuccess() {
        sendMessage(CoffeeResponseEnum.Success, BitMessage.build());
    }

    public void sendFailure(ResponseCode code) {
        sendMessage(code, BitMessage.build());
    }

    public void sendMessage(Long userId, BasicBinaryMessage message) {
        protocol.setData(message);
        protocol.setState(CoffeeResponseEnum.Success.code());
        sendMessage(userId, protocol);
    }

    public void sendMessage(List<Long> idList, BasicBinaryMessage message) {
        protocol.setData(message);
        protocol.setState(CoffeeResponseEnum.Success.code());
        sendMessage(idList, protocol);
    }

    public void sendMessageByAll(BasicBinaryMessage message) {
        protocol.setData(message);
        protocol.setState(CoffeeResponseEnum.Success.code());
        sendMessageByAll(protocol);
    }

}
